package com.im.linving.network.api;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @Author linyong
 * @Date 2016/10/31
 * @Time 18:08
 */
public class ApiRetrofitClient {

    public static final String BASE_FULI_URL = " https://api.netease.im/nimserver/ ";
    private static ApiRetrofitClient retrofitClient;

    /**
     *
     */
    private ApiRetrofitClient() {

    }

    /**
     * @return
     */
    public static ApiRetrofitClient builder() {
        if (null == retrofitClient) {
            instance();
        }
        return retrofitClient;
    }

    /**
     *
     */
    private static synchronized void instance() {
        retrofitClient = new ApiRetrofitClient();
    }

    /**
     * @return
     */
    public IMApi getServerApi() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //设置 Debug Log 模式
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);
        OkHttpClient okHttpClient = builder.build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_FULI_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofit.create(IMApi.class);
    }
}
